# -*- encoding:utf8 -*-

try:
    file = open("except.txt", "w")
    file.write("测试测试")
except (IOError, TypeError):
    print("ERROR:没有找到该文件")
else:
    print("写入文件成功！")
finally:
    print("关闭文件！")
    file.close()


def div(a, b):
    if b == 0:
        raise Exception("除数不能为0")
    return a / b


print("10除以5等于", div(10, 5))
try:
    print("5除以0等于", div(5, 0))
except Exception as args:
    print("算法异常，原因:", args)


# 自定义异常
class AlgorithmException(Exception):
    def __init__(self, arg):
        self.args = arg
        self.message = "算法异常"


try:
    raise AlgorithmException("算法异常，除数不能为0")
except AlgorithmException as e:
    print("异常:", e.message)
